package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.OffineActivityAdapter;
import com.wbao.dianniu.citylist.CitySelectedActivity;
import com.wbao.dianniu.data.ActivityListData;
import com.wbao.dianniu.listener.IActivityListListener;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.ActivityListManager;
import com.wbao.dianniu.update.ActivitySignupManager;
import com.wbao.dianniu.update.ActivitySignupManager.IActivitySignupListener;
import com.wbao.dianniu.utils.Notification;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 线下活动
 * 
 * @author yejianjun
 * 
 */
public class OfflineActivity extends Activity implements IActivityListListener,OnClickListener,IActivitySignupListener {
	private ActivityListManager manager;
	private PullToRefreshListView pullListView;
	private ListView listView;
	private final int PAGE_COUNTS = 10;
	private int currentPage = 0;
	private List<ActivityListData> mList = new ArrayList<ActivityListData>();
	private OffineActivityAdapter adapter;
	private TextView noDataTV;
	private String address = "";
	private static final int TYPE = 1;

	public static final String RET_DATE = "result_data";
	private final int REQUESTCODE = 1001;
	private TextView downTV;
	private ImageButton backBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			Utils.setTranslucentStatus(this,true);
			SystemBarTintManager tintManager = new SystemBarTintManager(this);
			tintManager.setStatusBarTintEnabled(true);
			tintManager.setStatusBarTintResource(R.color.title_bg);// 通知栏所需颜色
		}
		setContentView(R.layout.activity_offline_act);
		initView();
		Intent intent = getIntent();
		address = intent.getStringExtra(Const.BUNDLE_DATA);
		if (!TextUtils.isEmpty(address)) {
			downTV.setText(address);
		}
		initData();
		ActivitySignupManager.getInstance().addListener(this);
	}

	private void initView() {
		backBtn = (ImageButton) findViewById(R.id.offline_back_button);
		downTV = (TextView) findViewById(R.id.location_tv);
		noDataTV = (TextView) findViewById(R.id.no_find_data);
		pullListView = (PullToRefreshListView) findViewById(R.id.offline_list);
		pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
		pullListView.setOnRefreshListener(onRefreshListener);
		listView = pullListView.getRefreshableView();
		registerForContextMenu(listView);
		downTV.setOnClickListener(this);
		backBtn.setOnClickListener(this);
	}

	private void initData() {
		manager = new ActivityListManager(this);
		manager.addActivityListener(this);
		adapter = new OffineActivityAdapter(this);
		adapter.addData(mList);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(itemClickListener);
		reqData(0);
	}

	private void reqData(int page) {
		if(address.equals(getResources().getString(R.string.all_city))){
			manager.activityReq(GlobalContext.getAccountId(), page * PAGE_COUNTS,PAGE_COUNTS, "", TYPE);
		}else{
			manager.activityReq(GlobalContext.getAccountId(), page * PAGE_COUNTS,PAGE_COUNTS, address, TYPE);
		}
	}

	OnRefreshListener onRefreshListener = new OnRefreshListener() {

		@Override
		public void onRefresh() {
			// 上拉刷新 加载请求数据
			pullUpRequest();
		}
	};

	// 上拉刷新数据请求
	private void pullUpRequest() {
		// 没有搜索结果 或者数据为空 上拉没有效果
		currentPage++;
		reqData(currentPage);
	}

	OnItemClickListener itemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			 Intent intent = new Intent();
			 intent.putExtra(Const.INTENT_ACTIVITYID,mList.get(position-1).getId());
			 intent.setClass(OfflineActivity.this, ActivityDetail.class);
			 startActivity(intent);
		}
	};

	@Override
	public void onActivityFailure(int errorCode, String errorMsg) {
		pullListView.onRefreshComplete(false, false);
		Notification.toast(this, errorMsg);
	}

	@Override
	public void onActivitySuccess(List<ActivityListData> list) {
		pullListView.onRefreshComplete(false, false);
		if (null != list) {
			adapter.addData(list);
			adapter.notifyDataSetChanged();
			mList = adapter.getList();
			if (mList.size() == 0) {
				noDataTV.setVisibility(View.VISIBLE);
			} else {
				noDataTV.setVisibility(View.GONE);
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.location_tv:
			Intent intent = new Intent();
			intent.setClass(this, CitySelectedActivity.class);
			startActivityForResult(intent, REQUESTCODE);
			break;
		case R.id.offline_back_button:
			finish();
			break;
		default:
			break;
		}
	}
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(requestCode == REQUESTCODE ){
			if (null != data) {
				// 获得数据返回
				String city = data.getStringExtra(RET_DATE);
				if (null != city /*&& !city.equals("")*/) {
					refreshLocation(city);
				}
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	private void refreshLocation(String city){
		address = city;
		downTV.setText(city);
		adapter.clearData();
		currentPage= 0;
		reqData(currentPage);
	}

	@Override
	public void signupCountChanged(int activityId, int counts) {
		for (int i = 0; i < mList.size(); i++) {
			if(activityId == mList.get(i).getId()){
				mList.get(i).setInCount(counts);
				adapter.notifyDataSetChanged();
			}
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(null != manager){
			manager.removeActivityListener(this);
		}
		ActivitySignupManager.getInstance().removeListener(this);
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
}
